﻿using System;
using Excepciones;
using NHibernate;
using NHibernate.Cfg;
using Recursos;

namespace MJD.Transaccional.AccesoDatos.Implementacion.NHibernateMySql
{
    public class NHibernateHelper
    {
        #region Atributos

        private static log4net.ILog _log;

        private static readonly ISessionFactory _sessionFactory;

        #endregion

        #region Metodos

        /// <summary>
        /// Constructor estático en donde se inicializa la sesion de NHibernate
        /// </summary>
        static NHibernateHelper()
        {
            log4net.Config.BasicConfigurator.Configure();
            _log = log4net.LogManager.GetLogger(typeof(NHibernateHelper));

            try
            {
                Configuration cfg = new Configuration();
                _sessionFactory = cfg.Configure().BuildSessionFactory();
            }
            catch (Exception e)
            {
                _log.Error(e.Message);
                _log.Error(e.InnerException.Message);
                throw new ExcepcionInfoConfigNHibernate(
                    RecursoDAO.errConfigNHibernate, e);
            }
        }


        /// <summary>
        /// Metodo estático para solicitar la sesion de NHibernate
        /// </summary>
        /// <returns>retorna la sesion abierta</returns>
        public static ISession OpenSession()
        {
            return _sessionFactory.OpenSession();
        }


        /// <summary>
        /// Metodo que retorna la sesion abierta
        /// </summary>
        /// <returns>la sesion</returns>
        public static ISession GetCurrentSession()
        {
            return _sessionFactory.GetCurrentSession();
        }

        #endregion

    }
}
